package com.xiliu.rocketmq;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author xiliu
 * @description  消费者demo
 * @date 2022/3/3
 */
public class ConsumerDemo {

    public static void main(String[] args) throws Exception{
        // 推送型消费者
        DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer("xiliu_consumer_group");
        pushConsumer.setNamesrvAddr("42.194.222.32:9876");
        // 订阅topic，接收此Topic下的所有消息
        pushConsumer.subscribe("xiliu_topic", "*");

        pushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                /*for (MessageExt msg : msgs) {
                    try {
                        System.out.println(new String(msg.getBody(), "UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }*/
                System.out.println("收到消息->" + msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        pushConsumer.start();
    }
}
